﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Shell.DomainDot;
using Framework.Poco;
using Framework.Domain;

namespace Framework.Mvc.Controllers
{
    /// <summary>
    /// 图片控制器
    /// </summary>
    public class PictureController : Controller
    {
        private IDomainDotFactory factory;

        /// <summary>
        /// 构造器
        /// </summary>
        public PictureController()
        {
            factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 图片创建
        /// </summary>
        public ActionResult Create(Agency currentAgency, Guid identity, FilesUploaded filesUploaded)
        {
            if (currentAgency != null)
            {
                if (filesUploaded != null && filesUploaded.Identities.Count > 0)
                {
                    var context = factory.Create<PictureContext>(identity);
                    context.BindBinaryFile(filesUploaded.Identities);
                    return this.RedirectToAction("List", new { identity = identity });
                }
                return View();
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 图片修改
        /// </summary>
        [HttpPost]
        public ActionResult Edit(Agency currentAgency, PictureEditViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<PictureContext>(model.PictureCategoryId);
                context.Edit(model.PictureId, model.Rank, model.Description);
                return this.RedirectToAction("List", new { identity = model.PictureCategoryId });
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 图片删除
        /// </summary>
        [HttpGet]
        public ActionResult Delete(Agency currentAgency, PictureDeleteViewModel viewModel)
        {
            if (currentAgency != null)
            {
                return View(viewModel);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 图片删除
        /// </summary>
        [HttpPost]
        [ActionName("Delete")]
        public ActionResult DeletePost(Agency currentAgency, PictureDeleteViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<PictureContext>(model.PictureCategoryId);
                context.Delete(model.PictureId);
                return this.RedirectToAction("List", new { identity = model.PictureCategoryId });
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 图片列表
        /// </summary>
        public ActionResult List(Agency currentAgency, Guid identity)
        {
            if (currentAgency != null)
            {
                var pictureContext = factory.Create<PictureContext>(identity);
                var pictureCateogy = factory.Create<PictureCategoryContext>(currentAgency.Id);
                var model = new PictureListViewModel();
                model.List = pictureContext.GetAll();
                model.Category = pictureCateogy.GetPictureCategory(identity);
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }
    }
}
